<template>
  <div class="app-container">

    <u-page title="月租合同管理">

      <u-filtered>
        <el-form ref="listQuery" :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="申请人" prop="applicant">
            <el-input v-model="listQuery.applicant" placeholder="申请人" clearable />
          </el-form-item>
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="listQuery.orderNo" placeholder="订单号" clearable />
          </el-form-item>
          <el-form-item label="合同Id" prop="contractNo">
            <el-input v-model="listQuery.contractNo" placeholder="合同编号" clearable />
          </el-form-item>
          <el-form-item label="卡号" prop="memberId">
            <el-input v-model="listQuery.memberId" placeholder="卡号" clearable />
          </el-form-item>
          <el-form-item label="车牌" prop="plateNo">
            <el-input v-model="listQuery.plateNo" placeholder="车牌" clearable />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="listQuery.status" placeholder="状态" clearable>
              <el-option label="全部" value="" />
              <el-option label="有效" value="1" />
              <el-option label="失效" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="操作时间">
            <u-date-time-picker v-model="searchDate" value-format="yyyy-MM-dd HH:mm:ss" @change="changeSearchDate" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-dropdown>
              <el-button type="primary">
                批量操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :disabled="selectData.length===0" @click.native="hdlDel">批量删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="hdlDelete(row.id)">删除</el-button>
              <el-button v-if="row.status === 4 || row.status === 7 || row.status === 8" type="text" @click="hdlDownload(row)">下载</el-button>
              <el-button v-if="row.status === 4 || row.status === 7 || row.status === 8" type="text" @click="hdlInvalid(row)">作废</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <u-dialog title="作废合同" :show.sync="dialogFormVisible" @confirm="getInvalid">
      <el-form ref="dialogForm" :rules="rules" :model="invalidForm" label-width="120px">

        <el-form-item prop="applicant" label="抄送人">
          <el-input v-model="invalidForm.applicant" placeholder="抄送人" clearable />
        </el-form-item>

        <el-form-item prop="mobile" label="联系电话">
          <el-input v-model="invalidForm.mobile" placeholder="联系电话" clearable />
        </el-form-item>

        <el-form-item prop="reason" label="作废原因">
          <el-input v-model="invalidForm.reason" placeholder="作废原因" clearable show-word-limit />
        </el-form-item>
      </el-form>
    </u-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      api: this.$u.api.PluginsParkingContract,
      // 表格
      columns: [
        {
          prop: 'applicant',
          label: '申请人'
        },
        {
          prop: 'mobile',
          label: '联系人电话'
        },
        {
          prop: 'cardNo',
          label: '会员卡号'
        },
        {
          prop: 'orderNo',
          label: '订单号'
        },
        {
          prop: 'plateNo',
          label: '车牌'
        },
        {
          prop: 'contractName',
          label: '合同',
          render: (h, params) => {
            return h('div', {
              style: {
                color: '#409EFF',
                cursor: 'pointer',
                '&:hover': {
                  textDecoration: 'underline'
                }
              },
              on: {
                click: (e) => {
                  e.stopPropagation() // 阻止事件冒泡
                  this.handlePlateClick(params.row.contractNo) // 执行自定义方法
                }
              }
            }, params.row.contractName)
          }
        },
        {
          prop: 'contractNo',
          label: '合同Id'
        },
        {
          prop: 'status',
          label: '状态',
          render: (h, params) => {
            let title, type
            switch (params.row.status) {
              case 0:
                title = '草稿'
                type = 'warning'
                break
              case 1:
                title = '已撤回'
                type = 'info'
                break
              case 2:
                title = '签署中'
                type = 'primary'
                break
              case 3:
                title = '已退回'
                type = 'danger'
                break
              case 4:
                title = '已完成'
                type = 'success'
                break
              case 5:
                title = '已截止签署'
                type = 'info'
                break
              case 6:
                title = '拟定中'
                type = 'warning'
                break
              case 7:
                title = '签署失败'
                type = 'danger'
                break
              case 8:
                title = '作废中'
                type = 'primary'
                break
              case 9:
                title = '已作废'
                type = 'info'
                break
              case 10:
                title = '未知状态'
                break
            }
            return h('el-tag', { props: { type }}, title)
          }
        },
        {
          prop: 'beginTime',
          label: '开始时间'
        },
        {
          prop: 'endTime',
          label: '结束时间'
        },
        {
          prop: 'createdAt',
          label: '操作时间',
          timestamp: true
        }
      ],

      list: [],
      listLoading: true,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1
      },
      rules: {
        spaceNo: [
          { required: true, message: '请输入车位编号', trigger: ['blur'] }
        ]
      },
      dialogFormVisible: false,
      dialogStatus: '作废合同',
      dataForm: {},
      // 删除选中数据
      selectData: [],
      newForm: {
        status: 1
      },
      invalidForm: {
        applicant: '',
        mobile: '',
        contractNo: ''
      }
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    getInvalid() {
      this.api.invalid(this.invalidForm).then(res => {
        this.$u.msg(res.data)
        this.dialogFormVisible = false
        this.hdlList()
      })
    },
    hdlInvalid(e) {
      const { applicant, mobile, contractNo } = e
      this.invalidForm = { applicant, mobile, contractNo }
      this.dialogFormVisible = true
    },
    // 下载合同
    hdlDownload(e) {
      this.api.download(e.contractNo)
    },
    // 浏览合同
    handlePlateClick(e) {
      const data = {
        contractId: e
      }
      const self = this
      self.api.viewurl(data).then(res => {
        // console.log(res)
        window.open(res.data)
        /* const link = document.createElement('a')
        link.href = res.data
        link.target = '_blank'
        link.rel = 'noopener noreferrer'
        link.click() */
      })
    }
  }
}
</script>
